<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/calender.js"></script>
<link rel="stylesheet" href="../css/calendar.css"/>
<link rel="stylesheet" href="../css/calendar_month.css"/>
</head>
<body>

<%
 	Calendar cal = Calendar.getInstance(); //현재 시스템이 가지고 있는 날짜 데이터 가지고 오기
 	
 	int y=cal.get(Calendar.YEAR);
 	int m=cal.get(Calendar.MONTH)+1;
 	int d=cal.get(Calendar.DATE);

 	out.print("y = " + y + "<br>");
 	out.print("m = " + m + "<br>");
 	out.print("d = " + d + "<br>");
 	
 	
 	String _y = request.getParameter("y");
 	String _m = request.getParameter("m");
 	String _d = request.getParameter("d");

 	out.print("_y = " + _y + "<br>");
 	out.print("_m = " + _m + "<br>");
 	out.print("_d = " + _d + "<br>");
 	
 	if(_y!=null){
 		y=Integer.parseInt(_y);
 	}
 	if(_m!=null){
 		m=Integer.parseInt(_m);
 	}
 	if(_d!=null){
 		d=Integer.parseInt(_d);
 	}
 	
 	//y년 m월 1일의 요일
 	cal.set(y,m-1,1);


 	y=cal.get(Calendar.YEAR);
 	m=cal.get(Calendar.MONTH)+1;

 	out.print("y = " + y + "<br>");
 	out.print("m = " + m + "<br>");
 	
 	int w=cal.get(Calendar.DAY_OF_WEEK); //1(일)~7(토) => 일요일일때 w에 1. 메소드를 외우면 된다.

 	out.print("w = " + w + "<br>");
 %>

	<div id="contentGroup">
		<ul>
			<li><h2>전체 일정</h2></li>
			<hr>
		</ul>
	</div>

	<div id="content">

		<div id="calenderTab">
			<ul>
				<li><a href="#" onclick="showMonthlySchedule()">월간 일정</a></li>
				<li><a href="#" onclick="showWeeklySchedule()">주간 일정</a></li>
				<li><a href="#" onclick="showDailySchedule()">일간 일정</a></li>
			</ul>
		</div>

		<div id="calender">

			<table width="700" cellpadding="0" cellspacing="1" bgcolor="#cccccc">
				<td style="height: 20px; ">
					<select id="y" onchange="changeDate();">

						<%for (int i = y - 5; i <= y; i++) {%>
							<option value="<%=i%>"><%=i%>년</option>
						<%}%>
						
						<option value="<%=y%>" selected="selected"><%=y%>년</option>

						<%for (int i = y + 1; i <= y + 5; i++) {%>
							<option value="<%=i%>"><%=i%>년</option>
						<%}%>

					</select> 
					
					<select id="m" onchange="changeDate();">

						<%for (int i = 1; i <= 12; i++) {%>
							<option value="<%=i%>" <%=m == i ? "selected = 'selected'" : ""%>><%=i%>월</option>
						<%}%>

					</select>

				</td>

				<tr height="25">

					<td width="100" bgcolor="#c8c8c8" align="center">
						<font color="red">일</font>
					</td>

					<td width="100" bgcolor="#c8c8c8" align="center">월</td>

					<td width="100" bgcolor="#c8c8c8" align="center">화</td>

					<td width="100" bgcolor="#c8c8c8" align="center">수</td>

					<td width="100" bgcolor="#c8c8c8" align="center">목</td>

					<td width="100" bgcolor="#c8c8c8" align="center">금</td>

					<td width="100" bgcolor="#c8c8c8" align="center">
						<font color="blue">토</font>
					</td>

				</tr>

				<%
					//1일 앞부분 공백처리

					out.println("<tr height='25'>");

					for (int i = 1; i < w; i++) { // 6월의 1일은 토요일이라 w=7 따라서 공백 6번을 써준다.

						out.println("<td bgcolor='#ffffff'>&nbsp;</td>");
					}

					//1~마지막일 계산

					String fc;
					for (int i = 1; i <= cal.getActualMaximum(Calendar.DATE); i++) {

						fc = w % 7 == 1 ? "red" : (w % 7 == 0 ? "blue" : "black");	// 7로 나눠서 1이면 일요일/빨강 || 7로 나눠서 0이면 토요일/파랑  || 이외 평일/검정

						out.println("<td bgcolor='#ffffff' height='100' style='color:" + fc + ";'>");
						out.print("<a href='#' onclick='showCal(" + i +")'>" + i + "</a></td>");

						w++;
						if (w % 7 == 1 && i != cal.getActualMaximum(Calendar.DATE)) {	// 주 단위 나누기

							out.println("</tr>");
							out.println("<tr height='25'>");
						}
					}

					// 뒷부분 공백처리
					if (w % 7 != 1) {

						if (w % 7 == 0) {
							out.println("<td bgcolor='#ffffff'>&nbsp;</td>");
						} else {
							for (int i = w % 7; i <= 7; i++) {
								out.println("<td bgcolor='#ffffff'>&nbsp;</td>");
							}
						}

						out.println("</tr>");
						out.println("<tr height='100'>");// 맨아랫 부분 공백 

						for (int j = 1; j <= 7; j++) {
							out.println("<td align='center' bgcolor='#ffffff';>&nbsp;</td>");
						}

						out.println("</tr>");
					}
				%>
			</table>
		</div>
	</div>
	
	<%if(_d!=null){ %>
	<div>
	
		<div>
			<h2><%=_y %>년 <%=_m %>월 <%=_d %>일</h2>
		</div>
		
		<div>
			<h3></h3>
		</div>
		
		<div>
			<form name="scheduleInfo" action="" method="post">
				<input type="hidden" name="schedule_date" value="<%=_y%><%=_m%><%=_d%>"/>
				<input type="button" value="일정 추가" onclick="insert_schedule(<%=_y%>,<%=_m%>,<%=_d%>)"/>
				<input type="button" value="일정 수정" onclick="modify_schedule(<%=_y%>,<%=_m%>,<%=_d%>)" />
			</form> 
		</div>
	</div>
	<%} %>
</body>
</html>